
/********************************************************************

    My simple calculator.

    In this file are methods of Class 'Symbol_table'.

********************************************************************/

//-------------------------------------------

#include "std_lib_facilities.h"

//-------------------------------------------

#include "header.h"

//-------------------------------------------

using namespace std;

//-------------------------------------------



/********************************************

    User commands 'info', 'help'.

********************************************/

// Information function to display
// help information on the screen
void Symbol_table::variable_info( )
{
    cout << "\n------------------------------------------\n"
            << "\nIn this calculator you can use:"
            << "\n1. Floating point numbers."
            << "\n2. Following operators: "
            << "\n   '+'  - sum,"
            << "\n   '-'  - difference,"
            << "\n   '*'  - multiplication,"
            << "\n   '/'  - division,"
            << "\n   '%'  - modulo,"
            << "\n   '!'  - factorial an expression,"
            << "\n   '='  - an assignment "
            << "\n          (to the left of equal "
            << "\n          sign variable) or "
            << "\n          comparison (if the left "
            << "\n          number of expression)."
            << "\n3. Braces: "
            << "\n   '{...}' - figure (priority),"
            << "\n   '(...)' - round."
            << "\n4. Any custom variables"
            << "\n   starting with Latin letters"
            << "\n   containing Latin letters,"
            << "\n   numbers and underscope,"
            << "\n   in particular, constant variables:"
            << "\n   'pi' - number pi (3.1415926535),"
            << "\n   'e'  - number e (2.7182818284)."
            << "\n5. Function:"
            << "\n   'sqrt(...)'    - calculating "
            << "\n                    the square root,"
            << "\n   'pow(.,.)'     - number raised "
            << "\n                    to a power,"
            << "\n   'info', 'help' - getting help."
            << "\nPress 'Space' to separate solutions."
            << "\nPress 'Enter' to execute commands."
            << "\n\n------------------------------------------\n"
            << "\n\n";
}

//-------------------------------------------



/********************************************

    Methods of interaction
    width the user variables.

********************************************/

//-------------------------------------------

// Initialization of predefined variables
void Symbol_table::var_table_intial_value()
{
    //Variables v;
    // Number Pi (const)
    var_table_push_back( "pi", 3.1415926535 );
    // Number e (const)
    var_table_push_back( "e", 2.7182818284 );
    // Function  sqrt (const)
    var_table_push_back( "sqrt", 0 );
    // Function  pow (const)
    var_table_push_back( "pow", 0 );
    // Constant for the output
    // of help information to the screen
    var_table_push_back( "info" );
    var_table_push_back( "help" );
}

//-------------------------------------------

// Adding and editing user-defined variables
void Symbol_table::var_table_push_back( const string &name, double value )
{
    bool yesvar = false;
    for( int i = 0 ; i < var_table.size() ; ++i ){
        if( var_table[i].name == name ){
            yesvar  = true;
            var_table[i].value = value;
        }
    }
    if( yesvar == false ){
        Variables v;
        v.name  = name;
        v.value = value;
        var_table.push_back(v);
    }
}

//-------------------------------------------

// Adding and editing user-defined variables
void Symbol_table::var_table_push_back( const string &name )
{
    bool yesvar = false;
    for( int i = 0 ; i < var_table.size() ; ++i ){
        if( var_table[i].name == name ){
            yesvar = true;
        }
    }
    if( yesvar == false ){
        Variables v;
        v.name  = name;
        var_table.push_back(v);
    }
}

//-------------------------------------------

// Search the variables in the vector. Option 1
double Symbol_table::get_variable_value( const string &s )
{
    for( Variables vars : var_table ){
        if( vars.name == s ) return vars.value;
    }
    error( "get: not variable! ", s );
}

//-------------------------------------------

// Search the variables in the vector. Option 2
double Symbol_table::set_variable_value( const string &s )
{
    for( Variables vars : var_table ){
        if( vars.name == s ) return vars.value;
    }
    return 0;
}

//-------------------------------------------

// Search the variables in the vector and write new variables in the vector. Option 3
double Symbol_table::set_variable_value( const string &s, double d )
{
    for( Variables vars : var_table ){
        if( vars.name == s ) return vars.value;
    }
    Variables v;
    v.name  = s;
    v.value = d;
    var_table.push_back(v);
    return v.value;
}

//-------------------------------------------

// Display the contents of vector variable "var_table"
void Symbol_table::var_table_cout()
{
    cout << '\n';
    cout << "var_table.size() = " << var_table.size();
    cout << '\n';
    for( int i = 0 ; i < var_table.size() ; ++i ){
        cout << var_table[i].name << " = " << var_table[i].value << '\n';
    }
    cout << '\n';
}

//-------------------------------------------
